<?php
namespace app\api\controller;
use x\Xbase;
use x\G;
use x\Container;
class Index extends Xbase{

    public function init(){
        // 准许跨域请求。
        header("Access-Control-Allow-Origin: * ");
        header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
        if(is_post()){
            if(!$this->checkSign())
                err('签名错误');
            $d = $this->post;

            //调用接口
            $arr = array_merge(array_filter(explode("/",$d['url'])),[]);
            $arr[1] = ucfirst($arr[1]);
            $action = end($arr);
            $class = str_replace("/","\\","app\api\controller\\".$arr[0]."\\".$arr[1]);
            $container = Container::getInstance();
            $class = $container->make($class);
            $container->invokeMethod([$class, $action], []);
        }
        r(url('docs/index'));
    }
    //验签
    public function checkSign(){
        $d = $this->post;
        if($d['app_key']=='5300')
            return true;
        if(!isset($d['sign']) || !isset($d['app_key']) || $d['sign']!=sign($d, $d['app_key']))
            return false;
        return true;
    }
}
